package stud.aistar.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import stud.aistar.entity.*;
import stud.aistar.vo.AgeVo;

import java.util.List;

/**
 * @version 1.0
 * @Auther: git:jybdd - liming
 * @Description - 本类用于: 用户详情信息相关操作
 * @Date: 2019/9/28 13:07
 */

public interface UserInfoMapper {

    /**
     * 用于根据用户的id来获取User的详情信息 - 用户默认添加为0
     * @param id
     */
    @Select("select * from userinfo where user_id = #{value} and types = 0")
    Userinfo findByUserId(Integer id);

    /**
     * 查询就诊人信息
     * @param id
     * @return
     */
    @Select("select * from userinfo where user_id = #{value} and types = 1")
    List<Userinfo> findByTypes(Integer id);
    @Select("select id,uname,sex,age,rpad(left(idcard,6),18,'*') idcard,user_id,types from userinfo where user_id = #{value} and types = 1")
    List<Userinfo> findReplace(Integer id);

    @Update("update userinfo set types = 2 where id = #{id}")
    void delByUserInfoId(Userinfo userinfo);
    /**
     * 查询所有userinfo的id
     * @param id
     * @return
     */
    @Select("select * from userinfo where id = #{value}")
    Userinfo findByUserInfoId(Integer id);

    /**
     * 查询就诊人的总量
     * @param id
     * @return
     */
    @Select("select count(id) from userinfo where types = 1 and user_id = #{value}")
    Integer CountPeopleByUserId(Integer id);
    /**
     * 查询用户所有userinfo的信息
     * @param id
     * @return
     */
    @Select("select * from userinfo where user_id = #{value}")
    List<Userinfo> findAllByUserId(Integer id);

    /**
     * 用户添加个人资料,重要资料需要一次性填写完整 - 补充用户实名类型为0
     * @param userinfo
     */
    @Insert("insert into userinfo(uname,sex,age,idcard,user_id,types) values(#{uname},#{sex},#{age},#{idcard},#{userId},#{types})")
    void save(Userinfo userinfo);

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @Select("select * from user where id=#{id}")
    User findById(Integer id);

    /**
     * 在个人中心更改用户名,密码,手机号..
     * @param user
     */
    @Update("update user set username=#{username},password=#{password},phone=#{phone} where id=#{id}")
    void update(User user);

    @Insert("insert into user(username,password,phone,create_time) values(#{username},#{password},#{phone},#{createTime})")
    void saveUser(User user);


    @Select("select year(now()) nowyear,substring(#{str}, 7, 4) cardyear")
    AgeVo findCardYear(String str);

    @Select("select * from phone where id = #{id}")
    Phone findByPhoneId(Integer id);

    @Select("update phone set phone=#{phone},code=#{code} where id = #{id}")
    void updatePhone(Phone phone);

}
